package chat.network.http;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


/**
 * 发送消息
 * Created by 凯 on 2015/10/18.
 */
public class SendMessage {

    Delegate delegate;
    UserMessages userMessages;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setUserMessages(UserMessages userMessages) {
        this.userMessages = userMessages;
    }

    /**
     * 发送
     */
    public void send() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/Chat/SendMessages";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response,userMessages);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data",gson.toJson(userMessages));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    public interface Delegate
    {
        public void onResponse(String response,UserMessages userMessages);
        public void onErrorResponse(VolleyError error);
    }

    public static class UserMessages {
        private String user_FromID;
        private String user_ToID;
        private String message_Content;
        private String message_Time;
        private String message_Type;

        public String getUser_FromID() {
            return user_FromID;
        }
        public void setUser_FromID(String user_FromID) {
            this.user_FromID = user_FromID;
        }
        public String getUser_ToID() {
            return user_ToID;
        }
        public void setUser_ToID(String user_ToID) {
            this.user_ToID = user_ToID;
        }
        public String getMessage_Content() {
            return message_Content;
        }
        public void setMessage_Content(String message_Content) {
            this.message_Content = message_Content;
        }
        public String getMessage_Time() {
            return message_Time;
        }
        public void setMessage_Time(String message_Time) {
            this.message_Time = message_Time;
        }
        public String getMessage_Type() {
            return message_Type;
        }
        public void setMessage_Type(String message_Type) {
            this.message_Type = message_Type;
        }
    }

    public static class ResponseMessageState {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }


}
